package com.huangliusong.threadcurrency.example.syncContainer;

import com.google.common.collect.Sets;
import lombok.extern.slf4j.Slf4j;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.Vector;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

/**
 * @author huangliusong
 * @since 2018/11/20
 * {@link }
 */
@Slf4j
public class ConllectionExample1 {
    //请求总数
    public static int clientTotal=5000;
    //t
    public static int threadTotal=200;

    private static Set<Integer> list=Collections.synchronizedSet(Sets.newHashSet());

    public static void main(String[] args) throws InterruptedException {
        ExecutorService executorService=Executors.newCachedThreadPool();
        final Semaphore semaphore=new Semaphore(threadTotal);
        final CountDownLatch countDownLatch=new CountDownLatch(clientTotal);
        for (int i=0;i<clientTotal;i++){
            final  int count=i;
            executorService.execute(()->{

                try {
                    semaphore.acquire();
                    update1123(count);
                    semaphore.release();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                countDownLatch.countDown();
            });
        }
        countDownLatch.await();
        executorService.shutdown();
        log.info("size:{}",list.size());
    }

    private static  void update1123(int i){
        list.add(i);

    }
}
